﻿package mortal.game.view.shopMall
{
    import Message.DB.Tables.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import mortal.common.global.*;
    import mortal.common.tools.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.player.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.platform.*;
    import mortal.game.view.platform.platformTencent.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.shopMall.view.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ShopMallModule extends SpecialWindow
    {
        private var _currentShopCode:int;
        private var _currentType:int;
        private var _contentbg:ScaleBitmap;
        private var _xufeiMonthBtn:GLoadingButton;
        private var _tabBar:GTileList;
        private var _txtSearch:TextInputList;
        private var _searchButton:GButton;
        private var _currentPanel:ShopCommonPanel;
        protected var _rect3d:Rect3DObject;
        protected var _bodyPlayer:WizardActionPlayer;
        private var timerContorler:TimeControler;

        public function ShopMallModule(param1:ILayer = null)
        {
            this.timerContorler = TimeControler.createTimerInstance();
            this.timerContorler.unActive();
            setSize(778, 602);
            title = Language.getString(35571);
            this.layer = LayerManager.windowLayer;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            if (PlatformConst.isQQ() && PlatformTencentUtil.isBlueEnter())
            {
                this._xufeiMonthBtn = UIFactory.gLoadingButton(ResFileConst.QQBlueKaiTongBtn, 12, 549, 130, 37, this);
                this._xufeiMonthBtn.configEventListener(MouseEvent.CLICK, this.onClickHandle);
                this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.ShopMallText_1, 154, 547, this));
            }
            else
            {
                this._txtSearch = UIFactory.getUICompoment(TextInputList, 55, 549, this);
                this._txtSearch._textInput.width = 115;
                this._txtSearch._textInput.height = 25;
                this._txtSearch.init(this.onSearchTxtChange);
                this._searchButton = UIFactory.gButton(Language.getString(35573), 175, 551, 40, 22, this);
                this._searchButton.configEventListener(MouseEvent.CLICK, this.gotoPage);
                this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30067), 55, 572, 150, 22, this, GlobalStyle.textFormatBai));
            }
            this._tabBar = UIFactory.tileList(17, 40, 790, 80, this);
            this._tabBar.columnWidth = 80;
            this._tabBar.rowHeight = 54;
            this._tabBar.horizontalGap = 1;
            this._tabBar.verticalGap = 4;
            this._tabBar.setStyle("cellRenderer", ShopMallCellRender);
            this._tabBar.dataProvider = this.getBuyDataProvider(ShopUtil.getShopMallTab());
            this.addChild(this._tabBar);
            this._tabBar.configEventListener(Event.CHANGE, this.tabBarChangeHandler);
            this._tabBar.selectedIndex = 0;
            LoaderHelp.addResCallBack(ResFileConst.shopMall, this.showSkin);
            this.tabBarChangeHandler();
            this.add3d();
            return;
        }// end function

        private function onSearchTxtChange() : void
        {
            if (DebugUtil.isDebugCommand(this._txtSearch._textInput.text))
            {
                DebugUtil.runDebugCommand(this._txtSearch._textInput.text);
                return;
            }
            if (this._txtSearch._textInput.text.length >= 2)
            {
                this._txtSearch.updateDataProvider(this.getTxtListDp(this._txtSearch._textInput.text));
            }
            return;
        }// end function

        private function getTxtListDp(param1:String) : DataProvider
        {
            var _loc_4:ItemData = null;
            var _loc_5:* = undefined;
            var _loc_2:* = ShopUtil.getShopMallItems();
            var _loc_3:Array = [];
            for each (_loc_5 in _loc_2)
            {
                
                if (_loc_5)
                {
                    _loc_4 = new ItemData(_loc_5.itemCode);
                    if (String(_loc_4.name).indexOf(param1) == -1)
                    {
                        continue;
                    }
                    _loc_3.push(_loc_4.itemInfo);
                }
            }
            return new DataProvider(_loc_3);
        }// end function

        private function onClickHandle(event:MouseEvent) : void
        {
            if (event.target == this._xufeiMonthBtn)
            {
                GameUtil.gotoQQVipMonthUpgrade();
            }
            return;
        }// end function

        private function getBuyDataProvider(param1:Array) : DataProvider
        {
            var _loc_3:ShopTabData = null;
            var _loc_2:* = new DataProvider();
            for each (_loc_3 in param1)
            {
                
                _loc_2.addItem(_loc_3);
            }
            return _loc_2;
        }// end function

        private function showSkin() : void
        {
            if (isDisposed)
            {
                return;
            }
            this._contentbg = UIFactory.bg(10, 97, 760, 447, this, ImagesConst.ShopMallBg);
            this.addChildAt(this._contentbg, 2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallInit));
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            if (this._xufeiMonthBtn)
            {
                this._xufeiMonthBtn.dispose(param1);
                this._xufeiMonthBtn = null;
            }
            if (this._contentbg)
            {
                this._contentbg.dispose(param1);
                this._contentbg = null;
            }
            if (this._txtSearch)
            {
                this._txtSearch.dispose(param1);
                this._txtSearch = null;
            }
            if (this._searchButton)
            {
                this._searchButton.dispose(param1);
                this._searchButton = null;
            }
            if (this._currentPanel)
            {
                this._currentPanel.dispose(param1);
                this._currentPanel = null;
            }
            if (this._tabBar)
            {
                this._tabBar.dispose(param1);
                this._tabBar = null;
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            if (this._bodyPlayer)
            {
                this.timerContorler.unActive();
                FrEventDispatcher.instance.proxyRemoveEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._bodyPlayer.dispose(true);
                this._bodyPlayer = null;
            }
            this._currentType = 0;
            super.disposeImpl(param1);
            return;
        }// end function

        private function add3d() : void
        {
            this._rect3d = Rect3DManager.instance.getRect3dByWindow(this);
            var _loc_1:* = ModelConfig.instance.getInfoByCode(int(WizardConfig.instance.getSpriteByCodeAndLevel(1, 1).modelId));
            var _loc_2:* = _loc_1.mesh1 + ".md5mesh";
            var _loc_3:* = _loc_1.bone1 + ".skeleton";
            if (!this._rect3d)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(false, new Rectangle(0, 0, 736, 400), this);
            }
            Rect3DManager.instance.windowShowHander(null, this);
            if (this._bodyPlayer == null)
            {
                this._bodyPlayer = FrObjectPool.getObject(WizardActionPlayer, this);
                FrEventDispatcher.instance.proxyAddEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._bodyPlayer.timerContorler = this.timerContorler;
            }
            this.timerContorler.active();
            this._bodyPlayer.clearExtend();
            this._bodyPlayer.load(_loc_2, _loc_3, _loc_1.texture1, this._rect3d.renderList);
            if (_loc_1.mesh2)
            {
                _loc_2 = _loc_1.mesh2 + ".md5mesh";
                _loc_3 = _loc_1.bone2 + ".skeleton";
                this._bodyPlayer.loadExtend(_loc_2, _loc_3, _loc_1.texture2, this._rect3d.renderList);
                this._bodyPlayer.extendPlayer.timerContorler = this.timerContorler;
            }
            this._bodyPlayer.play();
            this._bodyPlayer.scaleValue = 4;
            this._bodyPlayer.rotationY = 0;
            this._rect3d.addObject3d(this._bodyPlayer, -60, 580, false);
            this._bodyPlayer.changeAction(ActionName.Standing);
            return;
        }// end function

        protected function onInitBodyPlayer(event:Event) : void
        {
            if (this._bodyPlayer)
            {
                this._bodyPlayer.addFrameComplete(ActionName.Standing, this.onFrameComplete);
            }
            return;
        }// end function

        private function onFrameComplete() : void
        {
            if (!this._bodyPlayer)
            {
                return;
            }
            this.setActionName(ActionName.Stand);
            return;
        }// end function

        public function setActionName(param1:String) : void
        {
            this._bodyPlayer.changeAction(param1);
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            if (_closeBtn)
            {
                _closeBtn.x = this.width - _closeBtn.width - 20;
                _closeBtn.y = 10;
            }
            return;
        }// end function

        private function addHotBuyPanel() : ShopRebateItemPanel
        {
            var _loc_1:* = UICompomentPool.getUICompoment(ShopRebateItemPanel);
            _loc_1.x = 0;
            _loc_1.y = 105;
            Dispatcher.dispatchEvent(new DataEvent(EventName.RebateShopGetPanicBuyConfig));
            return _loc_1;
        }// end function

        private function addGoodsList() : ShopMallPanel
        {
            var _loc_1:* = UICompomentPool.getUICompoment(ShopMallPanel);
            _loc_1.x = 0;
            _loc_1.y = 105;
            return _loc_1;
        }// end function

        private function addCopyShopPanel() : ShopCopyPanel
        {
            var _loc_1:ShopCopyPanel = null;
            _loc_1 = UICompomentPool.getUICompoment(ShopCopyPanel);
            _loc_1.x = 0;
            _loc_1.y = 105;
            return _loc_1;
        }// end function

        private function addGroupShopPanel(param1:Array) : ShopGroupPanel
        {
            var _loc_2:* = UICompomentPool.getUICompoment(ShopGroupPanel);
            _loc_2.setShopList(param1);
            _loc_2.x = 0;
            _loc_2.y = 105;
            return _loc_2;
        }// end function

        private function gotoPage(event:MouseEvent) : void
        {
            if (this._txtSearch._textInput.text == "")
            {
                return MsgManager.showRollTipsMsg(Language.getString(35574));
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallOpenAndSelect, this._txtSearch._textInput.text));
            return;
        }// end function

        public function getSelectedIndex() : int
        {
            if (this._tabBar == null)
            {
                return -1;
            }
            return this._tabBar.selectedIndex;
        }// end function

        private function tabBarChangeHandler(event:Event = null) : void
        {
            var _loc_3:Array = null;
            if (this._currentPanel)
            {
                this._currentPanel.dispose();
                this._currentPanel = null;
            }
            var _loc_2:* = this._tabBar.selectedItem as ShopTabData;
            this._currentShopCode = _loc_2.code;
            if (_loc_2.type == ShopTabData.typeRebateShop)
            {
                this._currentPanel = this.addHotBuyPanel();
            }
            else if (_loc_2.type == ShopTabData.typeCommonShop)
            {
                this._currentPanel = this.addGoodsList();
            }
            else if (_loc_2.type == ShopTabData.typeCopyShop)
            {
                this._currentPanel = this.addCopyShopPanel();
            }
            else if (_loc_2.type == ShopTabData.typeGroupShop)
            {
                _loc_3 = _loc_2.getItemListCallBack.apply();
                if (_loc_3.length)
                {
                    this._currentShopCode = (_loc_3[0] as TShop).code;
                }
                this._currentPanel = this.addGroupShopPanel(_loc_3);
            }
            if (_loc_2.type != ShopTabData.typeGroupShop)
            {
                this._currentPanel.getItemsByTabAndPage(this._currentShopCode, 1, this._currentType);
            }
            this.updateMoney();
            addChild(this._currentPanel);
            GameController.autoGuide.checkAndStopGuideShopMallWhenTabed();
            return;
        }// end function

        public function specialRefreshItems() : void
        {
            if (this._currentShopCode == 3008)
            {
                this._currentPanel.getItemsByTabAndPage(this._currentShopCode, 1, this._currentType);
            }
            return;
        }// end function

        public function updateMoney() : void
        {
            if (this._currentPanel)
            {
                this._currentPanel.updateMoney();
            }
            return;
        }// end function

        public function onUpdateRebate() : void
        {
            if (this._currentPanel is ShopRebateItemPanel)
            {
                (this._currentPanel as ShopRebateItemPanel).onUpdateRebate();
            }
            return;
        }// end function

        public function showAndSelectGood(param1:int, param2:int, param3:int, param4:int = 0) : void
        {
            if (param1 > (this._tabBar.dataProvider.length - 1))
            {
                return MsgManager.showRollTipsMsg(Language.getString(35575), MsgRollTipsType.msgRollTips3);
            }
            this._tabBar.selectedIndex = param1;
            this.tabBarChangeHandler();
            if (this._currentPanel is ShopGroupPanel)
            {
                (this._currentPanel as ShopGroupPanel).getItemsByTab2(param3, param4);
            }
            else
            {
                this._currentPanel.getItemsByTab(this._currentShopCode, this._currentType, param3);
            }
            return;
        }// end function

        public function changeTab(param1:int, param2:int = 0) : void
        {
            if (tabIndex > (this._tabBar.dataProvider.length - 1))
            {
                return MsgManager.showRollTipsMsg(Language.getString(35575), MsgRollTipsType.msgRollTips3);
            }
            this._tabBar.selectedIndex = param1;
            this.tabBarChangeHandler();
            if (this._currentPanel is ShopGroupPanel)
            {
                (this._currentPanel as ShopGroupPanel).getItemsByTab2(-1, param2);
            }
            return;
        }// end function

        public function shopShowArrow() : void
        {
            if (this._currentPanel is ShopRebateItemPanel)
            {
                (this._currentPanel as ShopRebateItemPanel).shopShowArrow();
            }
            return;
        }// end function

    }
}
